<?php
class EPMenuActiveProcessor extends EPMenuProcessor{
	public $activeItemCss='activeitem';
	public $activeLinkCss='activelink';
	public $activeListCss='activelist';
	public $activeParentItemCss='activeparentitem';
	public $activeParentListCss='activeparentlist';

	private $_controllerId;
	private $_actionId;

	public function afterParse($menuList){


	}

	public function beforeRender($menuList){
		$route=Yii::app()->getController()->getRoute();
		$this->processListRecursive($menuList,$route);
	}

	public function afterRender($menuList){

	}

	private function processListRecursive($menuList,$route){
		$menuItems=$menuList->getMenuItems();
		foreach($menuItems as $menuItem){
			if($this->isItemActive($menuItem->url,$route)){
				$menuItem->addItemCss($this->activeItemCss);
				$menuItem->addLinkCss($this->activeLinkCss);
				$parentList=$menuItem->getMenuList();
				$parentList->addMenuCss($this->activeListCss);
				while(($parentItem=$parentList->getParent())!==NULL){
					$parentItem->addItemCss($this->activeParentItemCss);
					
					$parentList=$parentItem->getMenuList();
					$parentList->addMenuCss($this->activeParentListCss);
				}
				return;
			}
				
			if($menuItem->hasSubMenu()){
				$submenu=$menuItem->getSubMenu();
				$this->processListRecursive($submenu,$route);
			}
		}
	}

	protected function isItemActive($itemUrl,$route)
	{
		if(!empty($itemUrl) && is_array($itemUrl) && !strcasecmp(trim($itemUrl[0],'/'),$route))
		{
			if(count($itemUrl)>1)
			{
				foreach(array_splice($itemUrl,1) as $name=>$value)
				{
					if(!isset($_GET[$name]) || $_GET[$name]!=$value)
						return false;
				}
			}
			return true;
		}
		return false;
	}
}